﻿Public Class VademecumDAO
    Public Shared Function insertarVademecum(ByVal ObraSocialXPlanId As Integer, ByVal MesId As Integer, ByVal VademecumAno As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Vademecum"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@VademecumId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@ObraSocialXPlanId", ObraSocialXPlanId).DbType = DbType.Int32
                    .AddWithValue("@MesId", MesId).DbType = DbType.Int32
                    .AddWithValue("@VademecumAno", VademecumAno).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerVademecums(ByVal VademecumId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_Vademecum_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@VademecumId", VademecumId).DbType = DbType.Int32
            da.Fill(ds, "Vademecums")

            Return ds.Tables("Vademecums")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarVademecum(ByVal VademecumId As Integer, ByVal ObraSocialXPlanId As Integer, ByVal MesId As Integer, ByVal VademecumAno As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Vademecum"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@ObraSocialXPlanId", ObraSocialXPlanId).DbType = DbType.Int32
                    .AddWithValue("@MesId", MesId).DbType = DbType.Int32
                    .AddWithValue("@VademecumAno", VademecumAno).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()

            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarVademecum(ByVal VademecumId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Vademecum"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@ObraSocialXplanId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@MesId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@VademecumAno", Constante.MENOSUNO).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With

            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function idVademecum(ByVal VademecumAno As Integer, ByVal MesId As Integer, ByVal ObraSocialXPlanId As Integer) As Integer
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_Vademecum_id"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@ObraSocialXplanId", ObraSocialXPlanId).DbType = DbType.Int32
                    .AddWithValue("@MesId", MesId).DbType = DbType.Int32
                    .AddWithValue("@VademecumAno", VademecumAno).DbType = DbType.Int32
                End With
                .Connection = cn
                Return .ExecuteScalar
            End With
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return -1
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function
End Class
